package me.ingxin.android.sample.lego.interceptor

import android.content.Context
import androidx.appcompat.app.AlertDialog
import me.ingxin.android.router.annotaions.Interceptor
import me.ingxin.android.router.route.RouteInterceptor
import me.ingxin.android.router.route.Request
import me.ingxin.android.sample.lego.utils.logD

/**
 * Created by ingxin on 2022/4/27
 */
@Interceptor("appGlobal3", global = true, priority = 4)
class Global3Interceptor : RouteInterceptor {
    override fun handle(context: Context, request: Request, dispatcher: RouteInterceptor.Dispatcher) {
        "Global3Interceptor开始执行".logD()
        //在目标路由是/activity/test1并且启动时携带了need_interrupt参数时，我们才提示是否拦截
        if (request.uri.path == "/activity/test1" && request.getBoolean("need_interrupt")) {
            AlertDialog.Builder(context)
                .setTitle("是否拦截跳转？")
                .setMessage("Global3Interceptor正在拦截${request.uri.path}")
                .setPositiveButton("拦截") { _, _ ->
                    dispatcher.interrupt()
                }
                .setNegativeButton("不拦截") { _, _ ->
                    dispatcher.continueRun()
                }
                .create()
                .show()
        } else {
            //其余情况不拦截，直接继续
            dispatcher.continueRun()
        }
    }
}